/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.gui.swing;

import com.sowrov.gui.model.tablemodel.JTableXEditorModel;

import java.awt.Color;
import java.awt.Component;

import java.util.Vector;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;


/**
 * In this table Every Cell can have a different cell editor, unlike JTable where cell editor could be set per
 * column
 *
 * @author Sowrov
 */
public class JTableX extends JTable {
    protected JTableXEditorModel cellEditorModel;

    public JTableX() {
        super();
        cellEditorModel = null;
    } // end JTableX()

    public JTableX(final TableModel tm) {
        super(tm);
        cellEditorModel = null;
    } // end JTableX()

    public JTableX(final TableModel tm, final TableColumnModel cm) {
        super(tm, cm);
        cellEditorModel = null;
    } // end JTableX()

    public JTableX(final TableModel tm, final TableColumnModel cm, final ListSelectionModel sm) {
        super(tm, cm, sm);
        cellEditorModel = null;
    } // end JTableX()

    public JTableX(final int rows, final int cols) {
        super(rows, cols);
        cellEditorModel = null;
    } // end JTableX()

    public JTableX(final Vector rowData, final Vector columnNames) {
        super(rowData, columnNames);
        cellEditorModel = null;
    } // end JTableX()

    public JTableX(final Object[][] rowData, final Object[] colNames) {
        super(rowData, colNames);
        cellEditorModel = null;
    } // end JTableX()

    // new constructor
    public JTableX(final TableModel tm, final JTableXEditorModel cellEditorModel) {
        super(tm, null, null);
        this.cellEditorModel = cellEditorModel;
    } // end JTableX()

    @Override
    public final TableCellEditor getCellEditor(final int row, final int col) {
        TableCellEditor tmpEditor = null;

        //System.out.println("In JTableX getCellEditor");
        if (cellEditorModel != null) {
            tmpEditor = cellEditorModel.getCellEditor(row, col);
        }

        if (tmpEditor != null) {
            //System.out.println("Return cellEditor at "+row+" "+col);
            return tmpEditor;
        } // end if

        return super.getCellEditor(row, col);
    } // end getCellEditor()

    public final void setCellEditorModel(final JTableXEditorModel cellEditorModel) {
        this.cellEditorModel = cellEditorModel;
    } // end setCellEditorModel()

    public final JTableXEditorModel getCellEditorModel() {
        return cellEditorModel;
    } // end getCellEditorModel()

    @Override
    public Component prepareRenderer(final TableCellRenderer renderer, final int Index_row, final int Index_col) {
        Component comp = super.prepareRenderer(renderer, Index_row, Index_col);

        //even index, selected or not selected
        if (((Index_row % 2) == 0) && !isCellSelected(Index_row, Index_col)) {
            comp.setBackground(Color.getHSBColor((float) 0, (float) 0, (float) 89.2));
        } // end if
        else if (!isCellSelected(Index_row, Index_col)) {
            comp.setBackground(Color.white);
        } // end else if

        return comp;
    } // end prepareRenderer()
} // end JTableX
